
package src.main.java.com.pms.service;

import src.main.java.com.pms.util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserService {

    /**
     * 用户登录
     */
    public boolean login(String username, String password) {
        if (username == null || username.trim().isEmpty() || password == null || password.trim().isEmpty()) {
            System.out.println("用户名和密码不能为空");
            return false;
        }

        String sql = "SELECT * FROM Users WHERE username = ? AND password = ? AND status = 1";
        try (ResultSet rs = JDBCUtil.executeQueryWithConnection(sql, username, password)) {
            if (rs.next()) {
                // 更新最后登录时间
                updateLastLoginTime(username);
                System.out.println("登录成功！");
                return true;
            } else {
                System.out.println("用户名或密码错误，或账号已被禁用");
                return false;
            }
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("登录过程中发生错误: " + e.getMessage());
            return false;
        }
    }

    /**
     * 用户注册
     */
    public boolean register(String username, String password) {
        if (username == null || username.trim().isEmpty() || password == null || password.trim().isEmpty()) {
            System.out.println("用户名和密码不能为空");
            return false;
        }

        // 检查用户名是否已存在
        if (isUsernameExists(username)) {
            System.out.println("用户名已存在");
            return false;
        }

        String sql = "INSERT INTO Users (username, password) VALUES (?, ?)";
        try {
            int result = JDBCUtil.executeUpdateWithParams(sql, username, password);
            if (result > 0) {
                System.out.println("注册成功！");
                return true;
            } else {
                System.out.println("注册失败");
                return false;
            }
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("注册过程中发生错误: " + e.getMessage());
            return false;
        }
    }

    /**
     * 修改密码
     */
    public boolean changePassword(String username, String oldPassword, String newPassword) {
        if (!login(username, oldPassword)) {
            System.out.println("原密码验证失败");
            return false;
        }

        String sql = "UPDATE Users SET password = ? WHERE username = ?";
        try {
            int result = JDBCUtil.executeUpdateWithParams(sql, newPassword, username);
            if (result > 0) {
                System.out.println("密码修改成功！");
                return true;
            } else {
                System.out.println("密码修改失败");
                return false;
            }
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("修改密码过程中发生错误: " + e.getMessage());
            return false;
        }
    }

    /**
     * 修改用户名
     */
    public boolean changeUsername(String oldUsername, String newUsername) {
        if (oldUsername == null || oldUsername.trim().isEmpty() || newUsername == null || newUsername.trim().isEmpty()) {
            System.out.println("用户名不能为空");
            return false;
        }

        // 检查新用户名是否已存在
        if (isUsernameExists(newUsername)) {
            System.out.println("新用户名已存在");
            return false;
        }

        String sql = "UPDATE Users SET username = ? WHERE username = ?";
        try {
            int result = JDBCUtil.executeUpdateWithParams(sql, newUsername, oldUsername);
            if (result > 0) {
                System.out.println("用户名修改成功！");
                return true;
            } else {
                System.out.println("用户名修改失败");
                return false;
            }
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("修改用户名过程中发生错误: " + e.getMessage());
            return false;
        }
    }

    /**
     * 检查用户是否是管理员
     */
    public boolean isAdmin(String username) {
        String sql = "SELECT role FROM Users WHERE username = ?";
        try (ResultSet rs = JDBCUtil.executeQueryWithConnection(sql, username)) {
            if (rs.next()) {
                return "ADMIN".equals(rs.getString("role"));
            }
            return false;
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("检查用户权限时发生错误: " + e.getMessage());
            return false;
        }
    }

    /**
     * 更新最后登录时间
     */
    private void updateLastLoginTime(String username) {
        String sql = "UPDATE Users SET last_login = CURRENT_TIMESTAMP WHERE username = ?";
        try {
            JDBCUtil.executeUpdateWithParams(sql, username);
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("更新登录时间时发生错误: " + e.getMessage());
        }
    }

    /**
     * 检查用户名是否已存在
     */
    private boolean isUsernameExists(String username) {
        String sql = "SELECT COUNT(*) FROM Users WHERE username = ?";
        try (ResultSet rs = JDBCUtil.executeQueryWithConnection(sql, username)) {
            if (rs.next()) {
                return rs.getInt(1) > 0;
            }
            return false;
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("检查用户名时发生错误: " + e.getMessage());
            return false;
        }
    }
}
